package com.example.mybubbleshooter;

import java.util.ArrayList;
import java.util.Random;

import android.graphics.Color;

public class Ball {
	private int ballColor;
	private float centerX;
	private float centerY;
	private float dy;
	private float dx;
	int fallingVariable = 0;
	static Random generator = new Random();
	boolean isStatic;
	public boolean isFalling;

	public float getCenterX() {
		return centerX;
	}

	public float getCenterY() {
		return centerY;
	}

	public void setCenterX(float x) {
		centerX = x;
	}

	public void setCenterY(float y) {
		centerY = y;
	}

	private Ball(int colorNum) {
		ballColor = colorNum;
	}

	public int getColor() {
		return ballColor;
	}

	public void setStatic(boolean isStatic) {
		this.isStatic = isStatic;
	}

	public boolean isSatatic() {
		return isStatic;
	}

	public void move() {
		if (!isStatic) {
			if (isFalling) {
				centerY += dy;
				centerX += dx;

			} else {
				centerX += dx;
				centerY -= dy;
			}
		}
	}

	public void setDx(float dx) {
		this.dx = dx;
	}

	public void setDy(float dy) {
		this.dy = dy;
	}

	public float getDx() {
		return dx;
	}

	public float getDy() {
		return dy;
	}

	public boolean isFalling() {
		return isFalling;
	}

	public void setFalling(boolean b) {
		isFalling = b;
	}

	public static Ball createANewBall() {
		switch (Global.currentLevel) {
		case Global.EASY:
			return getEasyBalls();
		case Global.MEDIUM:
			return getMediumBalls();
		case Global.HARD:
			return getHardBalls();
		}
		return null;
	}

	public static Ball[] getDefaultBalls() {
		Ball[] array = new Ball[Global.numOfInitializedBalls];
		for (int i = 0; i < Global.numOfInitializedBalls; i++) {
			int random = -1;
			switch (Global.currentLevel) {
			case Global.EASY:
				random = generator.nextInt(3);
				random += 2;// generate a number between 2-4 was 2
				break;
			case Global.MEDIUM:
				random = generator.nextInt(2);
				random += 1;// generate a number between 1-3
				break;
			case Global.HARD:
				random = generator.nextInt(1);
				random += 1;// generate a number between 1-2
				break;
			}
			Ball currentball = createANewBall();
			for (int j = 0; i < Global.numOfInitializedBalls && j < random; j++, i++) {
				array[i] = currentball;
				currentball = new Ball(currentball.ballColor);
			}
			i--;
		}
		return array;
	}

	/*
	 * Return a ball with one of four colors BLUE GREEN RED YELLOW
	 */
	private static Ball getEasyBalls() {
		int colorNum = -1;
		int random = generator.nextInt(4);
		switch (random) {
		case 0:
			colorNum = Color.BLUE;
			break;
		case 1:
			colorNum = Color.GREEN;
			break;
		case 2:
			colorNum = Color.RED;
			break;
		case 3:
			colorNum = Color.YELLOW;
			break;
		}

		return new Ball(colorNum);

	}

	/***
	 * We add two more Colors Black Magenta
	 */
	private static Ball getMediumBalls() {
		int colorNum = -1;
		int random = generator.nextInt(6);
		switch (random) {
		case 0:
			colorNum = Color.BLUE;
			break;
		case 1:
			colorNum = Color.GREEN;
			break;
		case 2:
			colorNum = Color.RED;
			break;
		case 3:
			colorNum = Color.YELLOW;
			break;
		case 4:
			colorNum = Color.BLACK;
			break;
		case 5:
			colorNum = Color.MAGENTA;
			break;
		}
		return new Ball(colorNum);
	}

	/***
	 * We add the two last colors Cyan Gray
	 * 
	 */
	private static Ball getHardBalls() {
		int colorNum = -1;
		int random = generator.nextInt(8);
		switch (random) {
		case 0:
			colorNum = Color.BLUE;
			break;
		case 1:
			colorNum = Color.GREEN;
			break;
		case 2:
			colorNum = Color.RED;
			break;
		case 3:
			colorNum = Color.YELLOW;
			break;
		case 4:
			colorNum = Color.BLACK;
			break;
		case 5:
			colorNum = Color.MAGENTA;
			break;
		case 6:
			colorNum = Color.CYAN;
			break;
		case 7:
			colorNum = Color.GRAY;
		}
		return new Ball(colorNum);

	}

	public static void addBallsToPool(int number) {
		for (int i = 0; i < number; i++)
			Global.pool.add(createANewBall());

	}

}
